home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Science / RLaB / examples / mgs.r < prev    next >
Text File  |  1994-04-25  |  615b  |  30 lines

  1. //
  2. // Modified Gram-Schmidt
  3. // Given A (MxN), with rank(A) = N. The following algorithm computes
  4. // the factorization A = Q*R (skinny QR) where Q (MxN) has orthonormal
  5. // columns and R (NxN) is upper triangular
  6. //
  7. // From MATRIX Computations, G.H. Golub, C.F. Van Loan (page 219)
  8. // 
  9.  
  10. mgs = function(A)
  11. {
  12.   local(a,k,j,n,m,q,r);
  13.  
  14.   a = A;
  15.   m = a.nr;
  16.   n = a.nc;
  17.   for(k in 1:n)
  18.     {
  19.       r[k;k] = norm( a[1:m;k], "2");
  20.       q[1:m;k] = a[1:m;k]/r[k;k];
  21.       for(j in k+1:n)
  22.     {
  23.       r[k;j] = q[1:m;k]' * a[1:m;j];
  24.           a[1:m;j] = a[1:m;j] - q[1:m;k] * r[k;j];
  25.         }
  26.      }
  27.   return << q = q; r = r >>;
  28. }
  29.  
  30.